<?php
/**
 * @title 1620. 网络信号最好的坐标 - 穷列
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer[][] $towers
     * @param Integer $radius
     * @return Integer[]
     */
    function bestCoordinate($towers, $radius) {
        /**
         * @since 2022-11-17 塔的范围(x1, y1), (x2, y2)
         */
        $x1 = $x2 = $towers[0][0];
        $y1 = $y2 = $towers[0][1];

        /**
         * @since 2022-11-18 找区间范围
         */
        foreach ($towers as list($x, $y, $s)){
            $x1 = min($x1, $x);
            $x2 = max($x2, $x);
            $y1 = min($y1, $y);
            $y2 = max($y2, $y);
        }

        /**
         * @since 2022-11-18 最大信号点
         */
        $x3 = $y3 = $s3 = 0;

        /**
         * @since 2022-11-18 穷列所有点
         */
        for($x=$x1; $x<=$x2; $x++){
            for($y=$y1; $y<=$y2; $y++){
                /**
                 * @since 2022-11-18 计算信号强度
                 */
                $s = 0;
                foreach ($towers as list($x4, $y4, $s4)){
                    /**
                     * @since 2022-11-18 距离
                     */
                    $d = sqrt(pow($x-$x4, 2) + pow($y-$y4, 2));

                    /**
                     * @since 2022-11-18 信号范围内
                     */
                    if($d <= $radius){
                        $s += intval($s4/(1+$d));
                    } else {}
                }

                /**
                 * @since 2022-11-18 选出最强信号
                 */
                if($s > $s3){
                    $s3 = $s;
                    $x3 = $x;
                    $y3 = $y;
                } elseif($s == $s3) {
                    if($x < $x3){
                        $x3 = $x;
                        $y3 = $y;
                    } elseif($x == $x3 AND $y < $y3) {
                        $y3 = $y;
                    } else {}
                } else {}
            }
        }

        /**
         * @return
         */
        return [$x3, $y3];
    }
}


/**
 * @url http://127.0.0.1/leetcode/202211/2022.11.02_2.php
 */
$datas = [
    [[[1,2,5],[2,1,7],[3,1,9]], 2], // [2,1]
    [[[0,1,2],[2,1,2],[1,0,2],[1,2,2]], 1], // [1,1]
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->bestCoordinate(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
